﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OBDUDSUtils.Mongoose;
using OBDUDSUtils.CNNMode;
using OBDUDSUtils.Common;

namespace DeviceTestStand
{
    public partial class Form1 : Form
    {
        private const string PLATFORM_FILE_NAME = "platforms";
        private MongooseManage mongoose;
        private DiagData device;
        private List<PlatformMessage> platformMessages;
        private PlatformMessage currentPlatform;
        private string platformFile;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Init();

            EventHandlers();
        }

        private void EventHandlers()
        {
            this.tool_abort.Click += Tool_abort_Click;
            this.tool_help.Click += Tool_help_Click;
            this.tool_link.Click += Tool_link_Click;
            this.tool_set.Click += Tool_set_Click;
            this.tool_close.Click += Tool_close_Click;

            this.menu_abort.Click += Menu_abort_Click;
            this.menu_file.Click += Menu_file_Click;
            this.menu_help.Click += Menu_help_Click;
            this.menu_platformManage.Click += Menu_platformManage_Click;
            this.menu_scriptConfig.Click += Menu_scriptConfig_Click;

            this.tb_barCode.TextChanged += Tb_barCode_TextChanged;
            this.lv_platform.SizeChanged += Lv_platform_SizeChanged;
            this.lv_platform.SelectedIndexChanged += Lv_platform_SelectedIndexChanged;

            this.mongoose.MonOBDMessageEvents += Mongoose_MonOBDMessageEvents;
        }

        private void Lv_platform_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.currentPlatform = this.platformMessages.Find(p => this.lv_platform.FocusedItem.Text == p.Platforms);
            if (this.currentPlatform == null)
                return;
            this.lbx_platform.Text = this.lv_platform.FocusedItem.Text;
            this.lv_script.Items.Clear();
            var scriptfile = System.Environment.CurrentDirectory + "\\data\\" + this.currentPlatform.ScriptName;
            var data = UpdatePlatform.ReadScriptMessage(scriptfile);
            foreach (var script in data)
            {
                this.lv_script.Items.Add(new ListViewItem(script));
            }
        }

        private void Lv_platform_SizeChanged(object sender, EventArgs e)
        {
            this.lv_platform.Columns[0].Width = this.lv_platform.Width - 10;
        }

        private void Menu_scriptConfig_Click(object sender, EventArgs e)
        {
            FrmScriptManager script = new FrmScriptManager();
            if (script.ShowDialog() == DialogResult.OK)
            { 
            }
        }

        private void Menu_platformManage_Click(object sender, EventArgs e)
        {
            FrmPlatformsManager platform = new FrmPlatformsManager(this.platformFile);
            if (platform.ShowDialog() == DialogResult.OK)
            { 
            }
        }

        private void Menu_help_Click(object sender, EventArgs e)
        {
            
        }

        private void Menu_file_Click(object sender, EventArgs e)
        {
            
        }

        private void Menu_abort_Click(object sender, EventArgs e)
        {
            
        }

        private void Tool_set_Click(object sender, EventArgs e)
        {

        }

        private void Tool_close_Click(object sender, EventArgs e)
        {
            this.mongoose.CloseMongoose(ref this.device);
        }

        private void Tool_link_Click(object sender, EventArgs e)
        {
            this.mongoose.OpenMongoose(ref this.device);
            this.mongoose.StartMsgFilter(this.device.MongooseDevice);
        }

        private void Tool_help_Click(object sender, EventArgs e)
        {
            
        }

        private void Tool_abort_Click(object sender, EventArgs e)
        {
            
        }

        private void Mongoose_MonOBDMessageEvents(List<CANBasic.VCI_CAN_OBJ> data)
        {
            throw new NotImplementedException();
        }

        private void Tb_barCode_TextChanged(object sender, EventArgs e)
        {
            var barCodeInput = this.tb_barCode.Text;
            if (!barCodeInput.Contains("\n"))
                return;
            barCodeInput = barCodeInput.Trim();//去掉换行符
            this.tb_barCode.Clear();

            //判断设备是否连接
            if (!this.device.MongooseDevice.MongooseConnected)
            {
                return;
            }
            //判断配置条件是否满足


            //判断条码是否与当前设备或平台匹配


            //执行脚本操作
            StartScript();
        }

        private void Init()
        {
            this.lv_platform.View = View.Details;
            this.lv_platform.FullRowSelect = true;
            this.lv_platform.HeaderStyle = ColumnHeaderStyle.None;
            this.lv_platform.Columns.Add("", this.lv_platform.Width - 10, HorizontalAlignment.Center);

            this.lv_script.View = View.Details;
            this.lv_script.FullRowSelect = true;
            this.lv_script.HeaderStyle = ColumnHeaderStyle.None;
            this.lv_script.Columns.Add("", this.lv_script.Width - 10, HorizontalAlignment.Center);

            this.platformMessages = new List<PlatformMessage>();
            this.mongoose = new MongooseManage();
            this.device = new DiagData();
            this.device.ZLGCANDevice = new ZLGCANConfig();
            this.device.ZLGCANDevice.DeviceCANID = 1;

            this.mongoose.InitCANDevice(ref this.device);

            this.platformFile = System.Environment.CurrentDirectory + "\\config\\";
            if (!System.IO.Directory.Exists(this.platformFile))
                System.IO.Directory.CreateDirectory(this.platformFile);
            this.platformFile += PLATFORM_FILE_NAME;

            this.platformMessages = UpdatePlatform.ReadPlatformMessage(this.platformFile);
            foreach (var plat in this.platformMessages)
            {
                this.lv_platform.Items.Add(new ListViewItem(plat.Platforms));
            }
        }

        private async void StartScript()
        {
            for (int i = 0; i < this.lv_script.Items.Count; i++)
            {
                var msg =this.lv_script.Items[i].Text;
                var buffer = DataConvert.StringConvert2Byte(msg);
                SendMessage(buffer);
                this.lv_script.FocusedItem = this.lv_script.Items[i];
                await Task.Delay(20);
            }
        }

        

        private void SendMessage(byte[] data)
        {
            this.device.ZLGCANDevice.Data = data;
            this.device.ZLGCANDevice.FrameID = 0x7df;

            bool result;
            this.mongoose.SendMessage(this.device.MongooseDevice, out result);
        }
    }
}
